<script setup>
import {onMounted, reactive, ref} from "vue";
import axios from "axios";
import {reqInstance} from "@/utils/request/index.js";
import {useRouter} from "vue-router";
import AddEdit from "@/views/customer/AddEdit.vue";
import dayjs from "dayjs";
import {useLoginStore} from "@/sotre/useLoginStore.js";
import {message} from "ant-design-vue";

const dataSource = ref([])
const r = useRouter()

const type2txt = [
    '私企',
    '国企',
    '政府单位',
    '其他',
]

const getData = async () => {
  try {
    const res = await reqInstance({
      url: "/customer?per_page="+pageSize.value+"&page="+current.value,
      method: "get",
    })
    dataSource.value = (res?.data?.data?.data ?? []).map((item) => {
      item.address_info = item.province + " - " + item.city + " - " + item.county + "\n" + item.address
      item.type_txt = type2txt[item.customer_type]
      item.industry_txt = item.industry_level_one + " - " + item.industry_level_two
      return item
    });
  } catch (e) {

  }
}

const columns = [
  {
    title: '企业名称',
    dataIndex: 'com_name',
  },
  {
    title: '社会统一信用代码',
    dataIndex: 'com_code',
  },
  {
    title: '地址',
    dataIndex: 'address_info',
  },
  {
    title: '客户性质',
    dataIndex: 'type_txt'
  },
  {
    title: '所属行业',
    dataIndex: 'industry_txt',
  },
  {
    title: '联系人姓名',
    dataIndex: 'market_user_txt',
  },
  {
    title: '联系人电话',
    dataIndex: 'tech_user_txt',
  },
  {
    dataIndex: 'action',
    title: '操作',
    key: 'action',
  },
]

const delOne = async (id) => {
  try {
    await reqInstance({
      url: "/customer/" + id,
      method: "delete",
    })
    message.success("删除成功")
    await getData()
  } catch (e) {

  }
}


const tableChange = (pagination)=>{
  current.value = pagination.current
  getData()
}

const sonDrawRef = ref()
const archiveDrawRef = ref()
const current = ref(1)
const pageSize = ref(10)
const total = ref(0)

onMounted(() => {
  getData()
})
</script>

<template>
  <a-button type="primary" @click="sonDrawRef.createOpen()">新增</a-button>
  <add-edit ref="sonDrawRef" @create-completed="getData()"/>
  <archive ref="archiveDrawRef" @archive-completed="getData()"/>
  <a-table :dataSource="dataSource" :columns="columns" :pagination="{
    current:current,
    pageSize:pageSize,
    total:total,
  }" @change="tableChange">
    <template #bodyCell="{ column, record }">
      <template v-if="column.dataIndex === 'pro_type_fees'">
        <!--        <ul>-->
        <!--          <template v-for="item in record.contract_project_type_fees">-->
        <!--            <li>{{ item.project_type_setting.type_title }} {{ item.fixed_amount }} {{ item.divide_amount }}</li>-->
        <!--          </template>-->
        <!--        </ul>-->
        <a-table :pagination="false"
                 :bordered="false"
                 style="margin: 0"
                 :dataSource="record.contract_project_type_fees" :columns="childColumns">

        </a-table>
      </template>
      <template v-if="column.dataIndex === 'action'">
        <a-button v-if="useLoginStore().isAdmin" class="edit-btn"
                  @click="sonDrawRef.updateOpen(record)">编辑
        </a-button>
        <a-button v-if="useLoginStore().isAdmin" @click="delOne(record.id)">删除</a-button>
      </template>
    </template>
  </a-table>


</template>

<style scoped>
.edit-btn {
  //padding-right: 5px;
  margin-right: 5px;
}
</style>